package com.koron.css2.newProjectCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.Enum.PjCollectionPaymentStatus;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 工程报装-工程收款-新增数据
 *
 * @author 朱超
 * @since 2022-01-19 10:44:39
 */
@Service
public class PjCollectionInsert implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean bean) {
        try {
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);

            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

            PjCollection pjCollection = JsonUtils.objectToPojo(bean.getData(), PjCollection.class);

            PjInfoBean pjInfo = pjInfoMapper.selectByBillNo(pjCollection.getProjectCode());
            if (pjInfo != null){
                pjCollection.setPjId(pjInfo.getId());
            }
            Map<String, String> prepayType = Tools.mapDicByCode(factory, "prepayType");
            if(pjInfo.getPrepayType()!=null){
                pjInfo.setPrepayType(prepayType.get(pjInfo.getPrepayType()));
            }
            Map<String, String> bearCost = Tools.mapDicByCode(factory, "bearCost");
            if(pjInfo.getBearCost()!=null ){
                pjInfo.setBearCost(bearCost.get( pjInfo.getBearCost()));
            }
            Map<String, String> noBearCost = Tools.mapDicByCode(factory, "noBearCost");

            if(pjInfo.getNoBearCost()!=null){
                pjInfo.setNoBearCost(noBearCost.get(  pjInfo.getNoBearCost()));
            }

            pjCollection.setCreateInfo(userInfo);

            pjCollection.setStatus(PjCollectionPaymentStatus.BEFORECOLLECTION.getStatus());

            pjCollectionMapper.insert(pjCollection);

            MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"工程收款新增成功", String.class);
            messageBean.setData(pjCollection.getId());
            return messageBean;
        } catch (Exception e) {
            logger.error("工程收款新增失败: ", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程收款新增失败", null);
        }
    }
}
